# MessageScheduler - Manages generating Messages to the message bus
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import random

from message import Message
from message_bus import MessageBus
from message_type_priority import MessageType, MessagePriority
from message_generator import MessageGenerator

class MessageScheduler(object):
    """
    MessageScheduler generates scheduled messages to the message bus.
    
    Adding a new message to scheduler doesn't emit message to message bus
    immediately. First scheduler waits random amount of time between 0-5min.
    After that scheduler waits set time interval. After this message is emitted
    to message bus between time intevals. So extra time is waited only once.
    This prevents all messages to be emitted at the same time to the bus.
    """

    def __init__(self, message_bus):
        """
        Create a MessageScheduler object
        @param message_bus: MessageBus where generated messages will be notified
        """
        self.message_bus = message_bus # Message bus where generated messages are notified.
        self.scheduled_messages = []   # List of tuples. Tuple is (Message, MessageGenerator (thread), interval)
        
    def addMessage(self, message, interval):
        """
        Schedule a new message  
        @param message: Message object
        @param interval: Time interval between generating message. (in seconds)
        """
        offset = random.randint(0, 300) # Random offset between 0-5 minutes
        msg_thread = MessageGenerator(message, self.message_bus, interval, offset)
        msg_thread.start()
        self.scheduled_messages.append((message, msg_thread, interval))
        
    def removeMessage(self, message):
        """
        Remove a scheduled message
        @param message: Remove this Message object from scheduling
        """
        for element in self.scheduled_messages:
            if element[0] is message:
                element[1].stopGenerator() # When sleep ends the thread will die
                del element
        
    def changeIntervalForMessage(self, message, new_interval):
        """
        Change the time interval for already scheduled message. Waiting starts
        at the beginning of new interval value.
        @param message: Message object which time interval want to be changed
        @param new_interval: New time interval for this message (in seconds)
        """
        for element in self.scheduled_messages:
            if element[0] is message:
                element[1].stopGenerator()
                t = MessageGenerator(message, self.message_bus, new_interval, 0)
                element[1] = t
